package com.ruoyi.oiltank.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oiltank.mapper.OilTankMapper;
import com.ruoyi.oiltank.domain.OilTank;
import com.ruoyi.oiltank.service.IOilTankService;

/**
 * 储油罐信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-21
 */
@Service
public class OilTankServiceImpl implements IOilTankService 
{
    @Autowired
    private OilTankMapper oilTankMapper;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询储油罐信息
     * 
     * @param id 储油罐信息ID
     * @return 储油罐信息
     */
    @Override
    public OilTank selectOilTankById(Long id)
    {
        return oilTankMapper.selectOilTankById(id);
    }

    @Override
    public void checkOilTank(OilTank oilTank) {

        Long id = oilTank.getId();
        String name = oilTank.getName();
        Long deptId = oilTank.getDeptId();
        OilTank oilTank1 = new OilTank();
        oilTank1.setId(deptId);
        oilTank1.setName(name);
        oilTank1.setId(id);
        List<OilTank> oilTanks = oilTankMapper.selectOilTankListOther(oilTank1);
        if (CollectionUtil.isNotEmpty(oilTanks)){
            throw new RuntimeException("该部门下已有同名储油罐！");
        }
        Long typeId = oilTank.getTypeId();
        BigDecimal maxCap = oilTank.getMaxCap();
        BigDecimal minCap = oilTank.getMinCap();
        BigDecimal weight = oilTank.getWeight();
        BigDecimal safeHeight = oilTank.getSafeHeight();
        BigDecimal height = oilTank.getHeight();
        if (typeId==3||typeId==4){
            String extra1 = oilTank.getExtra1();
            if (ObjectUtil.isNull(extra1)||new BigDecimal(extra1).compareTo(BigDecimal.ZERO)==0){
                throw new RuntimeException("污油罐必须维护预警高度");
            }
            String extra2 = oilTank.getExtra2();
            if (ObjectUtil.isNull(extra2)||new BigDecimal(extra2).compareTo(BigDecimal.ZERO)==0){
                throw new RuntimeException("污油罐必须维护报警高度");
            }
            int i = new BigDecimal(extra1).compareTo(new BigDecimal(extra2));
            if (i>0){
                throw new RuntimeException("污油罐预警高度必须小于报警高度");
            }

        }
//        if (minCap.compareTo(maxCap) > 0) {
//            throw new RuntimeException("["+name+"]储油罐最大装油量必须大于底油量");
//        }
//        if (typeId!=3){
//            if (weight.compareTo(minCap) < 0) {
//                throw new RuntimeException("["+name+"]储油罐存油量不得小于底油量");
//            }
//        }
//
//        if (ObjectUtil.isNotNull(weight)&&weight.compareTo(maxCap) > 0) {
//            throw new RuntimeException("["+name+"]储油罐存油量不得大于最大装油量");
//        }
//
//            if (ObjectUtil.isNotNull(height)) {
//                if (height.compareTo(safeHeight) > 0) {
//                    throw new RuntimeException("[" + name + "]储油罐当前油罐高度不得大于安全高度");
//                }
//            }




    }


    /**
     * 查询储油罐信息列表
     * 
     * @param oilTank 储油罐信息
     * @return 储油罐信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<OilTank> selectOilTankList(OilTank oilTank)
    {
        return oilTankMapper.selectOilTankList(oilTank);
    }

    /**
     * 新增储油罐信息
     * 
     * @param oilTank 储油罐信息
     * @return 结果
     */
    @Override
    public int insertOilTank(OilTank oilTank)
    {
        this.checkOilTank(oilTank);

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        oilTank.setCreateBy(loginUser.getUser().getUserName());
        oilTank.setDeptId(loginUser.getUser().getDeptId());
        oilTank.setCreateTime(DateUtils.getNowDate());
        return oilTankMapper.insertOilTank(oilTank);
    }

    /**
     * 修改储油罐信息
     * 
     * @param oilTank 储油罐信息
     * @return 结果
     */
    @Override
    public int updateOilTank(OilTank oilTank)
    {
        this.checkOilTank(oilTank);
        return oilTankMapper.updateOilTank(oilTank);
    }

    /**
     * 批量删除储油罐信息
     * 
     * @param ids 需要删除的储油罐信息ID
     * @return 结果
     */
    @Override
    public int deleteOilTankByIds(Long[] ids)
    {
        return oilTankMapper.deleteOilTankByIds(ids);
    }

    /**
     * 删除储油罐信息信息
     * 
     * @param id 储油罐信息ID
     * @return 结果
     */
    @Override
    public int deleteOilTankById(Long id)
    {
        return oilTankMapper.deleteOilTankById(id);
    }
}
